package edu.pmfbl.mnr.sem.mcmatm.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.borland.dbswing.*;
import com.borland.dx.dataset.*;

import edu.pmfbl.mnr.sem.mcmatm.app.*;
import edu.pmfbl.mnr.sem.mcmatm.beans.*;

/**
 * <p>Title: MCMA Test Maker</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: KrndijaSoft</p>
 *
 * @author Ninoslav Krndija
 * @version 1.0
 */
public class PitanjeNavPanel extends JPanel implements ListSelectionListener
{
  AppDataModule appDataModule = AppDataModule.getDataModule();
  public DataSet dataSet;  //INFO izgleda da mi nece trebati

  TableScrollPane pitanjaTsp = new TableScrollPane();
  JdbTable pitanjaTable = new JdbTable();
  JdbMyNavToolBar jdbNavToolBar1 = new JdbMyNavToolBar();

  public PitanjeNavPanel()
  {
    try
    {
      jbInit();
      setDataSet(appDataModule.getPitanjaDataSet());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public void jbInit() throws Exception
  {
    BorderLayout borderLayout = new BorderLayout();
    this.setLayout(borderLayout);

    jdbNavToolBar1.setFloatable(false);
    //jdbNavToolBar1.setAutoDetect(true);
    jdbNavToolBar1.setButtonStateDitto(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.setButtonStateSave(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.setButtonStateRefresh(JdbNavToolBar.HIDDEN);
    jdbNavToolBar1.getInsertButton().removeActionListener(jdbNavToolBar1);
    jdbNavToolBar1.getInsertButton().addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            insertButton_actionPerformed(e);
          }
        });
    jdbNavToolBar1.getPostButton().removeActionListener(jdbNavToolBar1);
    jdbNavToolBar1.getPostButton().addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            postButton_actionPerformed(e);
          }
        });
    jdbNavToolBar1.getDeleteButton().removeActionListener(jdbNavToolBar1);
    jdbNavToolBar1.getDeleteButton().addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            deleteButton_actionPerformed(e);
          }
        });
    jdbNavToolBar1.getCancelButton().removeActionListener(jdbNavToolBar1);
    jdbNavToolBar1.getCancelButton().addActionListener(new java.awt.event.ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            cancelButton_actionPerformed(e);
          }
        });
    pitanjaTable.setPopupMenuEnabled(false);
    pitanjaTable.setRowHeaderVisible(false);
    pitanjaTable.setEditable(false);
    pitanjaTable.getSelectionModel().addListSelectionListener(this);
    pitanjaTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    pitanjaTable.addMouseListener(new java.awt.event.MouseAdapter()
      {
        public void mouseClicked(MouseEvent e)
        {
          pitanjaTable_mouseClicked(e);
        }
      });
    //pitanjaTable.addKeyListener(KeyE);

    this.setPreferredSize(new Dimension(502, 180));
    this.add(jdbNavToolBar1, borderLayout.NORTH);
    this.add(pitanjaTsp, borderLayout.CENTER);

    pitanjaTsp.getViewport().add(pitanjaTable);
  }

  // Implementacija ListSelectionListener-a
  public void valueChanged(ListSelectionEvent e)
  {
  }

  public void setDataSet(DataSet d )
  {
    jdbNavToolBar1.setDataSet(d);
    pitanjaTable.setDataSet(d);
  }

  void insertButton_actionPerformed(ActionEvent e)
  {
    appDataModule.insertQuestion();

    //((MainFrame) this.getParent()).pitanjeInfoPanel.nazivField.requestFocus();
    PitanjeInfoPanel.getPitanjeInfoPanel().nazivField.requestFocus();
  }

  /*void insertButton_stateChanged(ChangeEvent e)
  {

  }*/

  void postButton_actionPerformed(ActionEvent e)
  {
    /**
     * Ako za trenutno pitanje nema odgovora (odgovoriDataSet prazan) to znaci
     * da je vrsen insert u pitanjaDataSet pa treba prikazati dijelog za uredjivanje
     * odgovora tj. treba dodati bar jedan tacan odgovor na trenutno pitanje
     * inace treba izvrsiti samo post i sacuvatiti promjene u izvoru podataka
     */
    //appDataModule.getDBExceptionHandler1().
    if(appDataModule.nemaTacnihOdgovora())
    {
      Component component = this;
      Frame frame = null;
      while((component = component.getParent()) != null)
      {
        if(component instanceof Frame)
        {
          frame = (Frame) component;
          break;
        }
      }
      if(frame == null)
        frame = new Frame();
      MessageDialog message = new MessageDialog(frame);
      message.setMessage("Svako pitanje mora imati bar jedan tačan odgovor.");
      message.setVisible(true);

        /*OdgovorEditDialog odgovorEditDialog = new OdgovorEditDialog(frame,
            "Uređivanje odgovora", true);
        odgovorEditDialog.show();*/
    }
    else
      appDataModule.saveQuestion();
  }

  void deleteButton_actionPerformed(ActionEvent e)
  {
    if(appDataModule.imaOdgovora())
    {
      Component component = this;
      Frame frame = null;
      while((component = component.getParent()) != null)
      {
        if(component instanceof Frame)
        {
          frame = (Frame) component;
          break;
        }
      }
      if(frame == null)
        frame = new Frame();
      MessageDialog message = new MessageDialog(frame);
      message.setMessage("Pitanje se može obrisati samo ako su svi odgovori obrisani.");
      message.setVisible(true);
    }
    /*else
    {
      appDataModule.getPitanjaDataSet().deleteRow();
      appDataModule.saveQuestion();
    }*/
  }

  void cancelButton_actionPerformed(ActionEvent e)
  {
    if(appDataModule.getUnosenjePitanja() && appDataModule.imaOdgovora())
    {
      Component component = this;
      Frame frame = null;
      while((component = component.getParent()) != null)
      {
        if(component instanceof Frame)
        {
          frame = (Frame) component;
          break;
        }
      }
      if(frame == null)
        frame = new Frame();
      MessageDialog message = new MessageDialog(frame);
      message.setMessage("Odustati od unosa pitanja se može samo ako su svi odgovori obrisani.");
      message.setVisible(true);
    }
    else
      appDataModule.getPitanjaDataSet().cancel();
  }

  void pitanjaTable_mouseClicked(MouseEvent e)
  {
    Component component = this;
    Frame frame = null;

    while((component = component.getParent()) != null)
    {
      if(component instanceof Frame)
      {
        frame = (Frame) component;
        break;
      }
    }

    if(frame == null)
      frame = new Frame();

    if(e.getClickCount() >= 2)  // treba 2
    {
      // ako je unosenjePitanja onda mozda insertovati red u odgovoriDataSet
      if(appDataModule.getOdgovoriDataSet().getRowCount() == 0)
        appDataModule.insertAnswer();
      OdgovorEditDialog odgovorEditDialog = new OdgovorEditDialog(frame, "Uređivanje odgovora", true);
      odgovorEditDialog.show();
    }
  }

}
